package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.system.dao.TFlupDao;
import com.ruoyi.system.domain.dto.PmsArchivesDto;
import com.ruoyi.system.domain.dto.TFlupRecordDto;
import com.ruoyi.system.domain.po.*;
import com.ruoyi.system.domain.vo.PmsArchivesVo;
import com.ruoyi.system.domain.vo.TFlupRecordVo;
import com.ruoyi.system.mapper.CmsArchivesCodeTagMapper;
import com.ruoyi.system.mapper.PmsArchivesRecordMapper;
import com.ruoyi.system.mapper.TFlupInfoMapper;
import com.ruoyi.system.mapper.TFlupRecordMapper;
import com.ruoyi.system.service.TCodeServier;
import com.ruoyi.system.service.TFlupRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * @author nanyang
 */
@Service
public class TFlupRecordServiceImpl implements TFlupRecordService {
    
    @Resource
    private TFlupInfoMapper flupInfoMapper;

    @Resource
    private TFlupRecordMapper flupRecordMapper;
    @Resource
    private CmsArchivesCodeTagMapper cmsArchivesCodeTagMapper;
    @Resource
    private TFlupDao tFlupDao;
    @Resource
    private TCodeServier codeServier;
    @Resource
    private PmsArchivesRecordMapper pmsArchivesRecordMapper;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertFlupRecord(TFlupRecordDto.Insert flupRecordInsert) {
        // 增加随访记录
        for (TFlupRecordDto.TFlupRecordDetailDto flupRecordDetailDto : flupRecordInsert.getDetailList()) {
            // 新增实体数据
            long flupRecordId = IdUtil.getSnowflake().nextId();
            TFlupRecord flupRecord = BeanUtil.copyProperties(flupRecordDetailDto, TFlupRecord.class);
            flupRecord.setFlupInfoId(flupRecordInsert.getFlupInfoId());
            flupRecord.setId(flupRecordId);
            flupRecord.setCreatedAt(new Date());
            flupRecordMapper.insertSelective(flupRecord);
        }
        // 将随访信息变为已随访
        TFlupInfo flupInfo = new TFlupInfo();
        flupInfo.setId(flupRecordInsert.getFlupInfoId());
        flupInfo.setFlupFlag((byte) 1);
        return flupInfoMapper.updateByPrimaryKeySelective(flupInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateFlupRecord(TFlupRecordDto.Update flupRecordUpdate) {
        int result = 0;
        for (TFlupRecordDto.TFlupRecordDetailDto flupRecordDetailDto : flupRecordUpdate.getDetailList()) {

            TFlupRecord flupRecord = BeanUtil.copyProperties(flupRecordDetailDto, TFlupRecord.class);
            flupRecord.setUpdatedAt(new Date());
            TFlupRecordExample flupRecordExample = new TFlupRecordExample();
            flupRecordExample.createCriteria().andDelFlagEqualTo((byte) 0)
                    .andFlupInfoIdEqualTo(flupRecordUpdate.getFlupInfoId())
                    .andCodeEqualTo(flupRecordDetailDto.getCode());
            result+= flupRecordMapper.updateByExampleSelective(flupRecord, flupRecordExample);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteFlupRecord(TFlupRecordDto.Delete flupRecordDelete) {
        int result = 0;
        for (String code : flupRecordDelete.getCodeList()) {
            TFlupRecord flupRecord = new TFlupRecord();
            flupRecord.setUpdatedAt(new Date());
            flupRecord.setDelFlag((byte) 2);
            TFlupRecordExample flupRecordExample = new TFlupRecordExample();
            flupRecordExample.createCriteria().andDelFlagEqualTo((byte) 0)
                    .andFlupInfoIdEqualTo(flupRecordDelete.getFlupInfoId())
                    .andCodeEqualTo(code);
            result+= flupRecordMapper.updateByExampleSelective(flupRecord, flupRecordExample);
        }
        return result;
    }

    @Override
    public List<TFlupRecordVo.Result> listFlupRecord(TFlupRecordDto.Query queryFlupRecord) {
        TFlupRecordExample flupRecordExample = new TFlupRecordExample();
        flupRecordExample.createCriteria().andDelFlagEqualTo((byte) 0).andFlupInfoIdEqualTo(queryFlupRecord.getFlupInfoId());
        List<TFlupRecord> flupRecordList = flupRecordMapper.selectByExample(flupRecordExample);
        return flupRecordList.stream().map(e -> BeanUtil.copyProperties(e, TFlupRecordVo.Result.class)).collect(Collectors.toList());
    }

    @Override
    public  PmsArchivesVo.Result queryDetail(PmsArchivesDto.Query dto) {
        if (dto.getTagId() != null) {
            //根据档案菜单查询变量
            CmsArchivesCodeTagExample cmsArchivesCodeTagExample = new CmsArchivesCodeTagExample();
            cmsArchivesCodeTagExample.createCriteria()
                    .andArchivesTagIdEqualTo(dto.getTagId()).andDelFlagEqualTo((byte) 0);
            cmsArchivesCodeTagExample.setOrderByClause("sort");
            List<CmsArchivesCodeTag> cmsArchivesCodeTagList = cmsArchivesCodeTagMapper.selectByExample(cmsArchivesCodeTagExample);
            if (CollectionUtil.isNotEmpty(cmsArchivesCodeTagList)) {
                List<String> collect = cmsArchivesCodeTagList.stream().map(CmsArchivesCodeTag::getCode).collect(Collectors.toList());
                dto.setCodeList(collect);
            }
        }
        //查询主数据
        List<PmsArchivesVo.Result> resultList = tFlupDao.selectInfo(CollectionUtil.newArrayList(dto.getId()), CollectionUtil.newArrayList(), dto.getTagId());

        //查询code
        List<TCode> codeList1 = codeServier.queryByCodeList(dto.getCodeList());
        Map<String, TCode> map = new HashMap<>();
        if (CollectionUtil.isNotEmpty(codeList1)) {
            map = codeList1.stream().collect(Collectors.toMap(TCode::getCode, Function.identity()));
        }
        for (PmsArchivesVo.Result result : resultList) {
            result.setRecordVoList(new ArrayList<>());
            //查询子数据
            PmsArchivesRecordExample pmsArchivesRecordExample = new PmsArchivesRecordExample();
            PmsArchivesRecordExample.Criteria criteria = pmsArchivesRecordExample.createCriteria().andDelFlagEqualTo((byte) 0).andArchivesIdEqualTo(result.getId());
            if (CollectionUtil.isNotEmpty(dto.getCodeList())) {
                criteria.andCodeIn(dto.getCodeList());
            }
            List<PmsArchivesRecord> pmsArchivesRecords = pmsArchivesRecordMapper.selectByExample(pmsArchivesRecordExample);
            List<PmsArchivesVo.PmsArchivesRecordVo> recordVoList = result.getRecordVoList();
            if (CollectionUtil.isNotEmpty(pmsArchivesRecords)) {
                recordVoList = BeanUtil.copyToList(pmsArchivesRecords, PmsArchivesVo.PmsArchivesRecordVo.class);
                for (PmsArchivesVo.PmsArchivesRecordVo pmsArchivesRecordVo : recordVoList) {
                    TCode tCode = map.get(pmsArchivesRecordVo.getCode());
                    if (tCode != null) {
                        pmsArchivesRecordVo.setCodeName(tCode.getText());
                        pmsArchivesRecordVo.setOptions(tCode.getOptions());
                        pmsArchivesRecordVo.setShowType(tCode.getShowType());
                        pmsArchivesRecordVo.setOperatorName(tCode.getOperatorName());
                    }
                }
            }

            List<PmsArchivesVo.PmsArchivesRecordVo> newList = new ArrayList<>();
//                if (CollectionUtil.isNotEmpty(recordVoList) && param.getTagId() != null) {
            List<String> codeList = recordVoList.stream().map(PmsArchivesVo.PmsArchivesRecordVo::getCode).collect(Collectors.toList());
            for (String code : dto.getCodeList()) {
                if (codeList.contains(code)) {
                    continue;
                }
                TCode tCode = map.get(code);
                PmsArchivesVo.PmsArchivesRecordVo pmsArchivesRecordVo = new PmsArchivesVo.PmsArchivesRecordVo();
                pmsArchivesRecordVo.setCode(code);
                if (tCode != null) {
                    pmsArchivesRecordVo.setOptions(tCode.getOptions());
                    pmsArchivesRecordVo.setShowType(tCode.getShowType());
                    pmsArchivesRecordVo.setOperatorName(tCode.getOperatorName());
                    pmsArchivesRecordVo.setCodeName(tCode.getText());
//                    pmsArchivesRecordVo.setSort();

                }
                newList.add(pmsArchivesRecordVo);
            }
            recordVoList.addAll(newList);

//            resultList.stream().sorted(Comparator.comparing());

            result.setRecordVoList(recordVoList);
            return result;
        }
        return null;
    }

}
